package com.rfid.a252robot.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.rfid.a252robot.R;
import com.rfid.a252robot.activity.adapter.BookItemAdapter;
import com.rfid.a252robot.activity.presenter.BorrowPresenter;
import com.rfid.a252robot.activity.view.IBorrowView;
import com.rfid.a252robot.base.BaseActivity;
import com.rfid.a252robot.base.BaseTitleActivity;
import com.rfid.db.bean.Book;
import com.rfid.mqtt.MqttManager;
import com.rfid.util.Util;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BorrowActivity  extends BaseActivity<BorrowPresenter> implements IBorrowView {


    public static void startActivity(Context context, String reader,String pwd,int boxNum) {
        Intent intent = new Intent();
        intent.putExtra("reader",reader);
        intent.putExtra("pwd",pwd);
        intent.putExtra("boxNum",boxNum);
        intent.setClass(context,BorrowActivity.class);
        context.startActivity(intent);
    }

    String mReader;
    int mBoxNum;

    @BindView(R.id.tip)
    TextView mTipView;
    @BindView(R.id.borrow_again)
    Button mBorrowAgain;
    @BindView(R.id.complete)
    Button mComplete;
    @BindView(R.id.ct)
    TextView mCTView;
    CountDownTimer mTimer;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    List<Book> mBooks = new ArrayList<>();
    BookItemAdapter mAdapter;


    @BindView(R.id.left_arrow)
    ImageView mLeft;
    @BindView(R.id.right_arrow)
    ImageView mRight;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_take_book);
        ButterKnife.bind(this);
        mBorrowAgain.setEnabled(false);
        mComplete.setEnabled(false);
        mReader = getIntent().getStringExtra("reader");
        String passWord = getIntent().getStringExtra("pwd");
        mBoxNum = getIntent().getIntExtra("boxNum",-1);
        String tip = mPresenter.borrow(mReader,passWord,mBoxNum);
        if (!TextUtils.isEmpty(tip)) {
            mTipView.setText(tip);
        }

        mTimer = new CountDownTimer(60000,1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                mCTView.setText((millisUntilFinished/1000) + "s");
            }

            @Override
            public void onFinish() {
                Intent intent = new Intent();
                intent.setClass(BorrowActivity.this,MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                finish();
            }
        };
        mTimer.start();
        mAdapter = new BookItemAdapter(mBooks);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        mRecyclerView.setAdapter(mAdapter);

        mLeft.setVisibility(View.GONE);
        mRight.setVisibility(View.GONE);
    }

    @Override
    protected int getCountDownTime() {
        return -1;
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (mBack != null) {
            mBack.setEnabled(false);
        }
    }

    @Override
    public BorrowPresenter getPresenter() {
        return new BorrowPresenter();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mTimer.cancel();
    }

    @OnClick(R.id.complete)
    public void complete(View v) {
        Intent intent = new Intent();
        intent.setClass(BorrowActivity.this,MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
    }

    @OnClick(R.id.borrow_again)
    public void borrowAgain(View v) {
        Util.startActivity(this,BookListActivity.class);
        finish();
    }

    @Override
    public void onBoxOpen(int boxNum, boolean s) {
        if (mBoxNum == boxNum) {
            String tip = "";
            if (!s) {
                tip = "对不起，柜门开启失败，请重新借阅";
                mTipView.setText(tip);
            } else {
                tip = Util.getBoxShow(boxNum) + "柜门已打开，请取走图书,取后请关门";
                mTipView.setText(tip);
                if (boxNum > 28) {
                    mLeft.setVisibility(View.VISIBLE);
                    ((AnimationDrawable)mLeft.getDrawable()).start();
                } else {
                    mRight.setVisibility(View.VISIBLE);
                    ((AnimationDrawable)mRight.getDrawable()).start();
                }
            }
            MqttManager.INSTANCE.sendVoice(tip);
        }
    }

    @Override
    public void onBoxClose(int boxNum) {
        mTipView.setText("柜门已关闭");
        mBorrowAgain.setEnabled(true);
        mComplete.setEnabled(true);
        if (mBack != null) {
            mBack.setEnabled(true);
        }
    }

    @Override
    public void onBorrowBook(Book book) {
        if (!mBooks.contains(book)) {
            mBooks.add(book);
            mAdapter.notifyDataSetChanged();
        }
    }
}
